
package character_10;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *InputStream and OutputStream
 * @author Administrator
 */
public class Character_10 {

    public static void main(String[] args) throws Exception {
        
        try {
            //dump(new FileInputStream("C:\\Users\\Administrator\\Desktop\\JavaLearningNotes\\.gitignore"), new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.txt"));
            
            URL url = new URL("https://www.baidu.com/");
            InputStream src = url.openStream();
            OutputStream dest = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.txt");
            dump(src, dest);
            //dumpBuffer(src, dest);
        } catch (IOException ex) {
            Logger.getLogger(Character_10.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        Member[] members = {new Member("101", "zs", 59), new Member("102", "ls", 60), new Member("103", "ww", 99)};
        for (Member member : members) {
            member.save();
        }
        
        System.out.println(Member.load("101"));
        System.out.println(Member.load("102"));
        System.out.println(Member.load("103"));
        
        Member2[] members2 = {new Member2("101", "zs", 59), new Member2("102", "ls", 60), new Member2("103", "ww", 99)};
        for (Member2 member2 : members2) {
            member2.save();
        }
        System.out.println(Member2.load("101"));
        System.out.println(Member2.load("102"));
        System.out.println(Member2.load("103"));
        
        FileReader reader = new FileReader("C:\\Users\\Administrator\\Desktop\\git使用方法.txt");
        StringWriter writer = new StringWriter();
        dumpReadWrite(reader, writer);
        System.out.println(writer.toString());
        
        dump(new FileInputStream("C:\\Users\\Administrator\\Desktop\\git使用方法.txt"), new FileOutputStream("C:\\Users\\Administrator\\Desktop\\git使用方法.java"), "UTF-8");
        
        
    }
    
    public static void dump(InputStream inputStream, OutputStream outputStream) throws IOException{
    
        try (InputStream i = inputStream; OutputStream out = outputStream;){
            byte[] data = new byte[1024];
            int length;
            while ((length = i.read(data)) != -1) {                
                out.write(data, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void dumpBuffer(InputStream inputStream, OutputStream outputStream) throws IOException{
    
        //使用缓冲区
        try (InputStream i = new BufferedInputStream(inputStream); OutputStream out = new BufferedOutputStream(outputStream);){
            byte[] data = new byte[1024];
            int length;
            while ((length = i.read(data)) != -1) {                
                out.write(data, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void dumpReadWrite(Reader src, Writer dest) throws IOException{
    
        try (Reader input = src; Writer output = dest;){
            char[] data = new char[1024];
            int len;
            while ((len = input.read(data)) != -1) {                
                output.write(data, 0, len);
            }
        }
    }
    
    public static void dump(InputStream src, OutputStream dest, String charset) throws IOException{
    
        dumpReadWrite(new InputStreamReader(src, charset), new OutputStreamWriter(dest, charset));
    }
    
    public static void dump2(InputStream src, OutputStream dest) throws IOException{
        
        dump(src, dest, System.getProperty("file.encoding"));
    }
}
